﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Windows.Forms;

namespace Manager.Common
{
    public static class CommonFunction
    {
        public static string AddSeparator(this double num)
        {
            var str = num.ToString();
            var sub = "";
            var sdecimal = "";
            if (str.Contains("-"))
            {
                sub = "-";
                str = str.Substring(1);
            }
            if (str.Contains("."))
            {
                sdecimal = str.Substring(str.IndexOf('.'));
                str = str.Substring(0, str.IndexOf('.'));
            }
            str = string.Join("", str.Replace(",", "").Reverse());
            var output = "";
            for (var i = 0; i <= str.Length - 1; i++)
            {
                output = str[i] + output;
                if ((i + 1) % 3 == 0 && (str.Length - 1) != i)
                    output = ',' + output;
            }
            output = sub + output + sdecimal;
            return output;
        }

        public static string AddSeparator(this int num)
        {
            var str = num.ToString();
            var sub = "";
            var sdecimal = "";
            if (str.Contains("-"))
            {
                sub = "-";
                str = str.Substring(1);
            }
            if (str.Contains("."))
            {
                sdecimal = str.Substring(str.IndexOf('.'));
                str = str.Substring(0, str.IndexOf('.'));
            }
            str = string.Join("", str.Replace(",", "").Reverse());
            var output = "";
            for (var i = 0; i <= str.Length - 1; i++)
            {
                output = str[i] + output;
                if ((i + 1) % 3 == 0 && (str.Length - 1) != i)
                    output = ',' + output;
            }
            output = sub + output + sdecimal;
            return output;
        }

        public static string AddSeparator(this string str)
        {
            if (string.IsNullOrEmpty(str))
                return "";
            var sub = "";
            var sdecimal = "";
            if (str.Contains("-"))
            {
                sub = "-";
                str = str.Substring(1);
            }
            if (str.Contains("."))
            {
                sdecimal = str.Substring(str.IndexOf('.'));
                str = str.Substring(0, str.IndexOf('.'));
            }
            str = string.Join("", str.Replace(",", "").Reverse());
            var output = "";
            for (var i = 0; i <= str.Length - 1; i++)
            {
                output = str[i] + output;
                if ((i + 1) % 3 == 0 && (str.Length - 1) != i)
                    output = ',' + output;
            }
            output = sub + output + sdecimal;
            return output;
        }

        public static double ParseDouble(this string value)
        {
            double dob = 0;
            value = value.Replace(",", "");
            try
            {
                dob = double.Parse(value, CultureInfo.InvariantCulture);
            }
            catch { }
            return dob;
        }

        public static long ParseLong(this string value)
        {
            long dob = 0;
            value = value.Replace(",", "");
            try
            {
                dob = long.Parse(value, CultureInfo.InvariantCulture);
            }
            catch { }
            return dob;
        }

        public static int ParseInt(this string value)
        {
            int dob = 0;
            value = value.Replace(",", "");
            try
            {
                dob = int.Parse(value, CultureInfo.InvariantCulture);
            }
            catch { }
            return dob;
        }

        public static byte[] encryptDataMD5(this string data)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] hashedBytes;
            System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
            hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(data));
            return hashedBytes;
        }

        public static DateTime ParseDateTime(this string str)
        {
            try
            {
                string[] format = {
                                      "dd/MM/yyyy",
                                      "MM/dd/yyyy",
                                      "ddMMyyyy",
                                      "MMddyyyy",
                                      "dMyy",
                                      "Mdyy",
                                      "dd-MM-yyyy",
                                      "MM-dd-yyyy",
                                      "yyyy-MM-dd",
                                      "d/M/yy",
                                      "M/d/yy"
                                  };
                return DateTime.ParseExact(str, format, CultureInfo.InvariantCulture, DateTimeStyles.None);
            }
            catch (Exception ex)
            {
                return DateTime.ParseExact("01/01/1753", "dd/MM/yyyy", CultureInfo.InvariantCulture);
            }
        }

        //tìm theo chiều rộng
        public static Control FindControl(Control root, string name)
        {
            if (root == null) throw new ArgumentNullException("root");
            var stack = new Stack<Control>();
            stack.Push(root);
            while (stack.Count > 0)
            {
                Control item = stack.Pop();
                if (item.Name == name) return item;
                foreach (Control child in item.Controls)
                {
                    if (child.Name == name) return child;
                    stack.Push(child);
                }
            }
            return null;
        }

        // tìm theo chiều sâu
        public static Control GetControlByName(Control root, string name, Control controlFound)
        {
            if (root == null) throw new ArgumentNullException("root");
            if (root.Name == name)
            {
                controlFound = root;
                return controlFound;
            }
            else
                foreach (Control child in root.Controls)
                {
                    controlFound = GetControlByName(child, name, controlFound);
                    if (controlFound != null)
                        return controlFound;
                }
            return null;
        }

        public static List<Control> GetListRoomByName(Control root, string name, List<Control> lstControl)
        {
            try
            {
                if (root == null) throw new ArgumentNullException("root");
                if (root.Name == name)
                {
                    lstControl.Add(root);
                }
                else
                    foreach (Control child in root.Controls)
                    {
                        lstControl = GetListRoomByName(child, name, lstControl);
                    }
                return lstControl;
            }
            catch { return new List<Control>(); }
        }
    }
}
